<template>
  <div class="releases-view">
    <div class="view-header">
      <h2>发布管理</h2>
      <el-button type="primary" @click="showCreateDialog" :disabled="products.length === 0">
        <el-icon><Plus /></el-icon>
        创建发布单
      </el-button>
    </div>

    <el-table :data="releases" style="width: 100%" v-loading="loading">
      <el-table-column prop="productId.name" label="产品" width="150" />
      <el-table-column prop="version" label="版本号" width="120" />
      <el-table-column prop="environment" label="环境" width="120">
        <template #default="scope">
          <el-tag :type="getEnvType(scope.row.environment)">
            {{ getEnvText(scope.row.environment) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="releasedBy" label="发布人" width="120" />
      <el-table-column prop="createdAt" label="创建时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="viewRelease(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 创建发布单对话框 -->
    <el-dialog v-model="createDialogVisible" title="创建发布单" width="500px">
      <el-form :model="newRelease" label-width="100px">
        <el-form-item label="产品" required>
          <el-select v-model="newRelease.productId" placeholder="选择产品">
            <el-option v-for="product in products" :key="product._id" :label="product.name" :value="product._id" />
          </el-select>
        </el-form-item>
        <el-form-item label="版本号" required>
          <el-input v-model="newRelease.version" placeholder="例如: 1.2.3" />
          <el-text type="info" size="small">格式: x.y.z (语义化版本)</el-text>
        </el-form-item>
        <el-form-item label="环境" required>
          <el-select v-model="newRelease.environment" placeholder="选择环境">
            <el-option label="开发环境" value="development" />
            <el-option label="测试环境" value="testing" />
            <el-option label="生产环境" value="production" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newRelease.notes" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="createDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="createRelease" :loading="creating">创建</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import api from '../api'

const releases = ref([])
const products = ref([])
const loading = ref(false)
const createDialogVisible = ref(false)
const creating = ref(false)

const newRelease = ref({
  productId: '',
  version: '',
  environment: 'production',
  notes: ''
})

const loadReleases = async () => {
  loading.value = true
  try {
    const result = await api.get('/releases')
    releases.value = result.data || []
  } catch (error) {
    ElMessage.error('加载发布列表失败')
  } finally {
    loading.value = false
  }
}

const loadProducts = async () => {
  try {
    const result = await api.get('/products')
    products.value = result.data || []
  } catch (error) {
    ElMessage.error('加载产品列表失败')
  }
}

const showCreateDialog = () => {
  newRelease.value = {
    productId: products.value[0]?._id || '',
    version: '',
    environment: 'production',
    notes: ''
  }
  createDialogVisible.value = true
}

const createRelease = async () => {
  if (!newRelease.value.productId || !newRelease.value.version) {
    ElMessage.warning('请填写产品、版本号和环境')
    return
  }

  creating.value = true
  try {
    await api.post('/releases', newRelease.value)
    ElMessage.success('发布单创建成功')
    createDialogVisible.value = false
    loadReleases()
  } catch (error) {
    ElMessage.error('创建发布单失败: ' + (error.response?.data?.message || error.message))
  } finally {
    creating.value = false
  }
}

const viewRelease = (release) => {
  ElMessage.info(`查看发布单: ${release.version} (功能开发中)`)
}

const getEnvType = (env) => {
  const map = {
    development: 'info',
    testing: 'warning',
    production: 'success'
  }
  return map[env] || 'info'
}

const getEnvText = (env) => {
  const map = {
    development: '开发',
    testing: '测试',
    production: '生产'
  }
  return map[env] || env
}

const getStatusType = (status) => {
  const map = {
    pending: 'warning',
    processing: 'info',
    success: 'success',
    failed: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = {
    pending: '待处理',
    processing: '处理中',
    success: '成功',
    failed: '失败'
  }
  return map[status] || status
}

const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleString('zh-CN')
}

onMounted(() => {
  loadProducts()
  loadReleases()
})
</script>

<style scoped>
.releases-view {
  padding: 20px;
}

.view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.view-header h2 {
  margin: 0;
}
</style>

